/*
* Copyright (c) 2014 Qunar.com. All Rights Reserved.
*/
package net.oschina.bilbodai.common.beanutil.property.asm.lexer.code;

import net.oschina.bilbodai.common.beanutil.property.asm.lexer.code.array.PrimeArrayLoadAppliers;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.LinkedList;

/**
 * 包含数组或者List的解析
 *
 * @author qt-maodai  Date: 15-8-5 Time: 下午10:56
 * @version $Id$
 */
public class ArrayCodeApplier extends BaseCodeApplier {

    private final int index;

    public ArrayCodeApplier(CodeApply codeApply, int index) {
        super(codeApply);
        this.index = index;
    }

    @Override
    protected CodeApplyResult applyCode(MethodVisitor mv, CodeApply codeApply) throws Exception {
        mv.visitIntInsn(Opcodes.BIPUSH, getIndex());
        Class<?> stackType = codeApply.getParentType().getComponentType();
        if (PrimeArrayLoadAppliers.isPrimeArrayType(codeApply.getParentType())) {
            stackType = PrimeArrayLoadAppliers.applyCode(codeApply.getParentType(), mv);
        } else {
            mv.visitInsn(Opcodes.AALOAD);
        }
        return new CodeApplyResult(codeApply.getParentType().getComponentType(), stackType, new LinkedList<Class<?>>());
    }

    public int getIndex() {
        return index;
    }
}